package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domain.convert.BriefSubjectConvert;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.handler.SubjectTypeHandler;
import com.jingdianjichi.subject.infra.basic.entity.SubjectBrief;
import com.jingdianjichi.subject.infra.basic.service.SubjectBriefService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Component
public class BriefTypeHandler implements SubjectTypeHandler {

    @Resource
    SubjectBriefService subjectBriefService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.BRIEF;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO){
//        List<SubjectBrief> subjectBriefList = new LinkedList<>();
//        subjectInfoBO.getOptionList().forEach(option ->{
//            SubjectBrief subjectBrief = BriefSubjectConvert.INSTABCE.convertBoToEntity(option);
//            subjectBrief.setSubjectId(subjectInfoBO.getId().intValue());
//            subjectBrief.setIsDeleted(IsDeletedFlagEnum.UN_DELETE.getCode());
//            subjectBriefList.add(subjectBrief);
//        });
//        subjectBriefService.batchInsert(subjectBriefList);
            SubjectBrief subjectBrief = BriefSubjectConvert.INSTABCE.convertBoToEntity(subjectInfoBO);
            subjectBrief.setSubjectId(subjectInfoBO.getId().intValue());
            subjectBrief.setIsDeleted(IsDeletedFlagEnum.UN_DELETE.getCode());
            subjectBriefService.insert(subjectBrief);
    }

    @Override
    public SubjectInfoBO query(Long subjectId) {
        SubjectBrief subjectBrief = new SubjectBrief();
        subjectBrief.setId(subjectId);
        SubjectBrief  result = subjectBriefService.queryByCondition(subjectBrief);
        String subjectAnswer = subjectBrief.getSubjectAnswer();
        SubjectInfoBO subjectInfoBO = new SubjectInfoBO();
        subjectInfoBO.setSubjectAnswer(subjectAnswer);
        return subjectInfoBO;
    }
}
